<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
  // Async函数基本使用

  function queryData () {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('tom')
      }, 1000)
    })
  }

  // async函数返回值是Promise实例对象，而不是具体的值
  async function getResult () {
    // queryData().then(ret => {
    //   console.log(ret)
    // })
    // 上面的Promise写法和下面的代码等效
    const ret = await queryData()
    // console.log(ret)
    // return ret
    // return new Promise((resolve, reject) => {
    //   resolve(ret)
    // })
    // return Promise.resolve(ret)
    // ------------------------------
    // return new Promise((resolve, reject) => {
    //   reject(ret)
    // })
    return Promise.reject(ret)
  }

  // data是Promise实例对象
  const data = getResult()
  data.then(ret => {
    console.log(ret)
  }).catch(err => {
    console.log(err)
  })

  </script>
</body>
</html>